import math


class Solution(object):
    def nthMagicalNumber(self, n, a, b):
        left = 1
        right = max(a, b) * n
        g = a * b // math.gcd(a, b)
        base = 10 ** 9 + 7

        def cal(val):
            ans = val // a + val // b - val // g
            return ans >= n

        while left < right:
            mid = (left + right) // 2
            if cal(mid):
                right = mid
            else:
                left = mid + 1
        return left % base


data = Solution()
n = 4
a = 2
b = 3
print(data.nthMagicalNumber(n, a, b))
